package com.qf.emp.service.impl;

import com.qf.emp.dao.UserDao;
import com.qf.emp.dao.impl.UserDaoImpl;
import com.qf.emp.entity.User;
import com.qf.emp.service.UserService;
import com.qf.emp.utils.DbUtils;

public class UserServiceImpl implements UserService{
    private UserDao userDao = new UserDaoImpl();
    //如果登录从管理员列表找到，传入信息，则登录成功，返回这个管理员。
    @Override
    public User login(String username, String passwpord) {
        User user = null;
        try {
            DbUtils.begin();
            User temp = userDao.selectUser(username);
            if(temp != null){
                if (temp.getPassword().equals(passwpord)){
                    user = temp;
                }
            }
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public boolean checkedUser(String username) {
        try {
            DbUtils.begin();
            User user= userDao.selectUser(username);
            if (user != null){
                return true;
            }
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }
        return false;
    }
    @Override
    public void registerUser(User user) {
        try {
            DbUtils.begin();
            userDao.insertUser(user);
            DbUtils.commit();
        } catch (Exception e) {
            DbUtils.rollback();
            e.printStackTrace();
        }
    }
}
